<template>
  <div class="forget-container">
    <div class="forget-box">
      <h2 class="title">找回密码</h2>
      
      <el-form v-if="!verified" :model="verifyForm" :rules="verifyRules" ref="verifyFormRef">
        <el-form-item prop="username">
          <el-input 
            v-model="verifyForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="phone">
          <el-input 
            v-model="verifyForm.phone"
            placeholder="请输入手机号"
            prefix-icon="Phone"
          />
        </el-form-item>

        <el-form-item prop="email">
          <el-input 
            v-model="verifyForm.email"
            placeholder="请输入邮箱"
            prefix-icon="Message"
          />
        </el-form-item>

        <div class="button-group">
          <el-button type="primary" @click="handleVerify" :loading="loading">验证</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
        </div>
      </el-form>

      <el-form v-else :model="passwordForm" :rules="passwordRules" ref="passwordFormRef">
        <el-form-item prop="password_hash">
          <el-input 
            v-model="passwordForm.password_hash"
            type="password"
            placeholder="请输入新密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <div class="button-group">
          <el-button type="primary" @click="handleUpdatePassword" :loading="loading">确认修改</el-button>
          <el-button @click="goToLogin">返回登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { verifyAccount, updatePassword } from '@/api/auth'

const router = useRouter()
const loading = ref(false)
const verified = ref(false)
const verifyFormRef = ref(null)
const passwordFormRef = ref(null)

const verifyForm = reactive({
  username: '',
  phone: '',
  email: '',
  is_admin: 1
})

const passwordForm = reactive({
  password_hash: ''
})

const verifyRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

const passwordRules = {
  password_hash: [
    { required: true, message: '请输入新密码', trigger: 'blur' }
  ]
}

const handleVerify = async () => {
  if (!verifyFormRef.value) return
  
  await verifyFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        const res = await verifyAccount(verifyForm)
        if (res.code === 1) {
          ElMessage.success('验证成功')
          verified.value = true
        } else {
          ElMessage.error(res.message || '验证失败')
        }
      } catch (error) {
        ElMessage.error('验证失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}

const handleUpdatePassword = async () => {
  if (!passwordFormRef.value) return
  
  await passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        const res = await updatePassword(verifyForm.username, passwordForm)
        if (res.code === 1) {
          ElMessage.success('密码修改成功')
          router.push('/login')
        } else {
          ElMessage.error(res.message || '密码修改失败')
        }
      } catch (error) {
        ElMessage.error('密码修改失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.forget-container {
  margin-left: -230px;
  width: 180vh;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.forget-box {
  width: 400px;
  padding: 30px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.button-group {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
</style> 